package com.orange.serviceUcenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.orange.commonutils.jwt.JwtUtils;
import com.orange.commonutils.md.MD5;
import com.orange.serviceUcenter.entity.UcenterMember;
import com.orange.serviceUcenter.entity.vo.LoginVo;
import com.orange.serviceUcenter.entity.vo.RegisterVo;
import com.orange.serviceUcenter.mapper.UcenterMemberMapper;
import com.orange.serviceUcenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orange.servicebase.exception.MyException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author orange
 * @since 2021-12-24
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    @Override
    public String login(LoginVo loginVo) {
        //获取邮箱和密码
        String email = loginVo.getEmail();
        String password = loginVo.getPassword();
        //判断输入的邮箱和密码是否为空
        if (StringUtils.isEmpty(password) || StringUtils.isEmpty(email)) {
            throw new MyException(20001, "邮箱或密码为空");
        }
        //判断邮箱是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("email", email);
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);
        if (ucenterMember == null) {
            throw new MyException(20001, "该邮箱不存在");
        }
        //判断密码是否正确
        // MD5加密是不可逆性的，不能解密，只能加密
        //将获取到的密码经过MD5加密与数据库比较
        if (!MD5.encrypt(password).equals(ucenterMember.getPassword())) {
            throw new MyException(20001, "密码不正确");
        }
        //判断用户是否禁用
        if (ucenterMember.getIsDisabled()) {
            throw new MyException(20001, "用户被禁用");
        }
        //生成jwtToken
        return JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());
    }

    @Override
    public void register(RegisterVo registerVo) {
        //获取前端传来的数据
        String nickname = registerVo.getNickname(); //昵称
        String code = registerVo.getCode(); //验证码
        String email = registerVo.getEmail(); //邮箱
        String password = registerVo.getPassword(); //密码
        //非空判断
        if (StringUtils.isEmpty(nickname)
                || StringUtils.isEmpty(code)
                || StringUtils.isEmpty(email)
                || StringUtils.isEmpty(password)) {
            throw new MyException(20001, "传来的数据有空值，注册失败");
        }
        //判断验证码
        //根据邮箱从redis获取验证码
        String redisCode = redisTemplate.opsForValue().get(email);
        if (!code.equals(redisCode)) {
            throw new MyException(20001, "验证码不正确");
        }
        //用户表中邮箱不能重复
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("email", email);
        Integer count = baseMapper.selectCount(wrapper);
        if (count >= 1) {
            throw new MyException(20001, "该邮箱已被注册");
        }
        //数据添加到数据库中
        UcenterMember member = new UcenterMember();
        //密码md5加密
        member.setPassword(MD5.encrypt(password));
        member.setEmail(email);
        member.setNickname(nickname);
        //默认不禁用用户
        member.setIsDisabled(false);
        //默认头像，阿里云oss存储
        member.setAvatar("https://orangestart.oss-cn-chengdu.aliyuncs.com/avatar/default");
        baseMapper.insert(member);
    }

    //查询数据库是否存在相同的微信内容
    @Override
    public UcenterMember getMemberByOpenId(String openid) {
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid", openid);
        return baseMapper.selectOne(queryWrapper);
    }

    //根据日期，获取那天注册人数
    @Override
    public Integer getCountRegister(String day) {
        return baseMapper.getCountRegister(day);
    }
}
